import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/contact.dart';

class ContactService {
  static const String _key = 'contacts';
  
  // 保存联系人列表
  Future<void> saveContacts(List<Contact> contacts) async {
    final prefs = await SharedPreferences.getInstance();
    final String data = json.encode(
      contacts.map((contact) => contact.toJson()).toList(),
    );
    await prefs.setString(_key, data);
  }
  
  // 获取联系人列表
  Future<List<Contact>> getContacts() async {
    final prefs = await SharedPreferences.getInstance();
    final String? data = prefs.getString(_key);
    
    if (data == null) return [];
    
    final List<dynamic> jsonData = json.decode(data);
    return jsonData.map((json) => Contact.fromJson(json)).toList();
  }
} 